********************************************************************************
* Dataanalysis: Workplace networks and political selection *********************
********************************************************************************

*This do-file include the code for all analyses in the main paper
*by Aggeborn and Andersson
*The analyses on dynamic effects (event study) is however found in the 
*robustness do-file, as this was initially made as a robustness check 
*for the online appendix  


clear all
clear matrix
set more off
set maxvar 120000
set mat 10000
cd "E:/ProjData/Workplace, elite/"
capture log close
log using "C:/Userdata/Shared/Logs/Workplace, elite/dataanalysis.text", replace text									
do "C:/Userdata/Shared/Dofiles/DoData/Workplace, elite/programs.do"		


use "finaldata.dta", clear

********************************************************************************
* Basic set-ups ****************************************************************
********************************************************************************

global covs "yearseducation zink"												
global wpssykcovslag "wpssyk_zink_lag wpssyk_yearseducation_lag wpssyk_gender_lag wpssyk_immig_lag"
global esttabbasic "replace label se nogaps b(5) compress booktabs nonotes star(* 0.1 ** 0.05 *** 0.01) varlabels(_cons "Constant") eqlabels(none)"
global depvar "i_nominated"
global indvar "maintreatvar"
global restriction "i_nominated_lag != 1 & i_nominated_lag2 != 1 & i_nominated_lag3 != 1 & i_nominated_lag4 != 1 & i_nominated_lag5 != 1" 	/*First time someone becomes a politician*/					

egen identificationvar = group(LopNr wp occupation) 							// Main variable for identification



********************************************************************************
* Numbers used in the text ***************************************************** 
********************************************************************************

sum m_wpssyk_nominated_lag s_wpssyk_nominated_lag, detail

********************************************************************************
* Main analysis **************************************************************** 
********************************************************************************

eststo clear
eststo: reg $depvar $indvar if $restriction, cluster(identificationvar)
estadd ysumm 
estadd local modelt "LPM"
estadd local indwpoccu "No"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe $depvar $indvar if $restriction, absorb(identificationvar) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local covs "No"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe $depvar $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe $depvar $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: clogit $depvar $indvar if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm 
estadd local modelt "C.logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'

eststo: clogit $depvar $indvar $covs $wpssykcovslag if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm
estadd local modelt "C.logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'



#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/main.tex", 
$esttabbasic keep($indvar) eform(0 0 0 0 1 1) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 pr2 N, fmt(4 0 0 0 0 0 0 0 0 3 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Pseudo-R2" "Observations")) 
;
#delimit cr	




********************************************************************************
* Partisan effects *************************************************************
********************************************************************************


** Center-right and center-left **
global partisanindvar "crtreatvar cltreatvar"
foreach k in CR CL {
	local depvar "i_nominated_`k'"

eststo clear
eststo: reg `depvar' $partisanindvar if $restriction, cluster(identificationvar)
estadd ysumm 
estadd local modelt "LPM"
estadd local indwpoccu "No"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' $partisanindvar if $restriction, absorb(identificationvar) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local covs "No"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' $partisanindvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' $partisanindvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: clogit `depvar' $partisanindvar if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm 
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'

eststo: clogit `depvar' $partisanindvar $covs $wpssykcovslag if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/nom_`k'.tex", 
$esttabbasic keep($partisanindvar) eform(0 0 0 0 1 1) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 pr2 N, fmt(4 0 0 0 0 0 0 0 0 3 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Pseudo-R2" "Observations")) 
;
#delimit cr	


}

gen cltreatvar_no_s = cltreatvar-Streatvar
	local depvar "i_nominated_S"

eststo clear
eststo: reg `depvar' cltreatvar_no_s Streatvar crtreatvar if $restriction, cluster(identificationvar)
estadd ysumm 
estadd local modelt "LPM"
estadd local indwpoccu "No"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' cltreatvar_no_s Streatvar crtreatvar if $restriction, absorb(identificationvar) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local covs "No"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' cltreatvar_no_s Streatvar crtreatvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' cltreatvar_no_s Streatvar crtreatvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: clogit `depvar' cltreatvar_no_s Streatvar crtreatvar if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm 
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'

eststo: clogit `depvar' cltreatvar_no_s Streatvar crtreatvar $covs $wpssykcovslag if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/nom_S.tex", 
$esttabbasic keep(cltreatvar_no_s Streatvar crtreatvar) eform(0 0 0 0 1 1) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 pr2 N, fmt(4 0 0 0 0 0 0 0 0 3 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Pseudo-R2" "Observations")) 
;
#delimit cr	


gen crtreatvar_no_m = crtreatvar-Mtreatvar
	local depvar "i_nominated_M"

eststo clear
eststo: reg `depvar' crtreatvar_no_m Mtreatvar cltreatvar if $restriction, cluster(identificationvar)
estadd ysumm 
estadd local modelt "LPM"
estadd local indwpoccu "No"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' crtreatvar_no_m Mtreatvar cltreatvar if $restriction, absorb(identificationvar) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local covs "No"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' crtreatvar_no_m Mtreatvar cltreatvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "No"
estadd local wptrend "No"

eststo: reghdfe `depvar' crtreatvar_no_m Mtreatvar cltreatvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: clogit `depvar' crtreatvar_no_m Mtreatvar cltreatvar if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm 
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "No" 
estadd local muni "No" 
estadd local mandate "No"
estadd local covs "No"
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'

eststo: clogit `depvar' crtreatvar_no_m Mtreatvar cltreatvar $covs $wpssykcovslag if $restriction, group(identificationvar) cluster(identificationvar) or 
estadd ysumm
estadd local modelt "C. logit"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "No"
estadd local muni "No" 
estadd local occutrend "No"
estadd local wptrend "No"
estadd scalar pr2 `e(r2_p)'


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/nom_M.tex", 
$esttabbasic keep(crtreatvar_no_m Mtreatvar cltreatvar) eform(0 0 0 0 1 1) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 pr2 N, fmt(4 0 0 0 0 0 0 0 0 3 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Pseudo-R2" "Observations")) 
;
#delimit cr	





********************************************************************************
* Ability mechanism ************************************************************
********************************************************************************


global compindvar "lowtreatvar hightreatvar"
foreach k in incom com {
	local depvar "i_nominated_`k'"

	eststo clear
	eststo: reg `depvar' $compindvar if $restriction, cluster(identificationvar)
	estadd ysumm
	estadd local modelt "LPM"
	estadd local indwpoccu "No"
	estadd local wpssykcovs "No" 
	estadd local muni "No" 
	estadd local mandate "No"
	estadd local covs "No"
	estadd local occutrend "No"
	estadd local wptrend "No"

	eststo: reghdfe `depvar' $compindvar if $restriction, absorb(identificationvar) cluster(identificationvar)
	estadd ysumm 
	estadd local modelt "LPM"
	estadd local indwpoccu "Yes"
	estadd local wpssykcovs "No" 
	estadd local muni "No" 
	estadd local mandate "No"
	estadd local covs "No"
	estadd local occutrend "No"
	estadd local wptrend "No"
	
	eststo: reghdfe `depvar' $compindvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod Kkod) cluster(identificationvar)
	estadd ysumm 
	estadd local modelt "LPM"
	estadd local indwpoccu "Yes"
	estadd local wpssykcovs "Yes" 
	estadd local muni "Yes" 
	estadd local mandate "Yes"
	estadd local covs "Yes"
	estadd local occutrend "No"
	estadd local wptrend "No"
	
	eststo: reghdfe `depvar' $compindvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
	estadd ysumm 
	estadd local modelt "LPM"
	estadd local indwpoccu "Yes"
	estadd local wpssykcovs "Yes" 
	estadd local muni "Yes" 
	estadd local mandate "Yes"
	estadd local covs "Yes"
	estadd local occutrend "Yes"
	estadd local wptrend "Yes"
	
	eststo: clogit `depvar' $compindvar if $restriction, group(identificationvar) cluster(identificationvar) or
	estadd ysumm 
	estadd local modelt "C. logit"
	estadd local indwpoccu "Yes"
	estadd local wpssykcovs "No" 
	estadd local muni "No" 
	estadd local mandate "No"
	estadd local covs "No"
	estadd local occutrend "No"
	estadd local wptrend "No"
	estadd scalar pr2 `e(r2_p)'

	eststo: clogit `depvar' $compindvar $covs $wpssykcovslag if $restriction, group(identificationvar) cluster(identificationvar) or 
	estadd ysumm
	estadd local modelt "C. logit"
	estadd local indwpoccu "Yes"
	estadd local wpssykcovs "Yes" 
	estadd local covs "Yes"
	estadd local mandate "No"
	estadd local muni "No" 
	estadd local occutrend "No"
	estadd local wptrend "No"
	estadd scalar pr2 `e(r2_p)'


	
	#delimit ;
	esttab using "C:/Userdata/Shared/Output/Workplace, elite/nom_`k'.tex", 
	$esttabbasic keep($compindvar) eform(0 0 0 0 1 1) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 pr2 N, fmt(4 0 0 0 0 0 0 0 0 3 3 0) 
	label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Pseudo-R2" "Observations")) 
	;
	#delimit cr

}


********************************************************************************
* List position cathegories: LPM ***********************************************
********************************************************************************


*In t*
label var top_al "Top"															// This classification is based on Buisserets et al (2022) Party Nomination Strategies in List Proportional Representation Systems, AJPS 
label var safe_al "Safe"
label var cmod_ab_al "Advantage" 
label var chigh_al "Highly" 
label var cmod_bel_al "Disad." 
label var sloss_al "Cert.Loss." 

eststo clear
eststo: reghdfe top_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq1 = _b[$indvar]
gen sdq1 = _se[$indvar]
gen N1 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe safe_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq2 = _b[$indvar]
gen sdq2 = _se[$indvar]
gen N2 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_ab_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq3 = _b[$indvar]
gen sdq3 = _se[$indvar]
gen N3 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe chigh_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq4 = _b[$indvar]
gen sdq4 = _se[$indvar]
gen N4 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_bel_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq5 = _b[$indvar]
gen sdq5 = _se[$indvar]
gen N5 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: reghdfe sloss_al $indvar $covs $wpssykcovslag if $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
gen bq6 = _b[$indvar]
gen sdq6 = _se[$indvar]
gen N6 = e(N)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/listposition.tex", 
$esttabbasic keep($indvar) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 N, fmt(4 0 0 0 0 0 0 0 0 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Observations")) 
;
#delimit cr	


*In t+1*

global restrictionlead "i_nominated_lag2 != 1 & i_nominated_lag3 != 1 & i_nominated_lag4 != 1 & i_nominated_lag5 != 1" 	/*First time someone becomes a politician*/					
foreach k in top_al safe_al cmod_ab_al chigh_al cmod_bel_al sloss_al {
	bysort LopNr (mandatperiod): gen `k'_lead = `k'[_n+1] 
}

label var top_al_lead "TopL1"															// This classification is based on Buisserets et al (2022) Party Nomination Strategies in List Proportional Representation Systems, AJPS 
label var safe_al_lead "SafeL1"
label var cmod_ab_al_lead "AdvantageL1" 
label var chigh_al_lead "HighlyL1" 
label var cmod_bel_al_lead "DisadL1." 
label var sloss_al_lead "Cert.LossL1" 

eststo clear
eststo: reghdfe top_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe safe_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_ab_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe chigh_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_bel_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: reghdfe sloss_al_lead $indvar $covs $wpssykcovslag if $restrictionlead, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/listposition_lead1.tex", 
$esttabbasic keep($indvar) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 N, fmt(4 0 0 0 0 0 0 0 0 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Observations")) 
;
#delimit cr	

*t+2*

global restrictionlead2 "i_nominated_lag3 != 1 & i_nominated_lag4 != 1 & i_nominated_lag5 != 1" 	/*First time someone becomes a politician*/					
foreach k in top_al safe_al cmod_ab_al chigh_al cmod_bel_al sloss_al {
	bysort LopNr (mandatperiod): gen `k'_lead2 = `k'[_n+2] 
}

label var top_al_lead2 "TopL2"															// This classification is based on Buisserets et al (2022) Party Nomination Strategies in List Proportional Representation Systems, AJPS 
label var safe_al_lead2 "SafeL2"
label var cmod_ab_al_lead2 "AdvantageL2" 
label var chigh_al_lead2 "HighlyL2" 
label var cmod_bel_al_lead2 "DisadL2." 
label var sloss_al_lead2 "Cert.LossL2" 

eststo clear
eststo: reghdfe top_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe safe_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_ab_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe chigh_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


eststo: reghdfe cmod_bel_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"

eststo: reghdfe sloss_al_lead2 $indvar $covs $wpssykcovslag if $restrictionlead2, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)
estadd ysumm
estadd local modelt "LPM"
estadd local indwpoccu "Yes"
estadd local wpssykcovs "Yes" 
estadd local covs "Yes"
estadd local mandate "Yes"
estadd local muni "Yes" 
estadd local occutrend "Yes"
estadd local wptrend "Yes"


#delimit ;
esttab using "C:/Userdata/Shared/Output/Workplace, elite/listposition_lead2.tex", 
$esttabbasic keep($indvar) stats(ymean modelt indwpoccu wpssykcovs covs mandate muni occutrend wptrend r2 N, fmt(4 0 0 0 0 0 0 0 0 3 0) 
label("Mean dep. var." "Regression model" "Individual*wp*occupation FE" "WP occupation covs t-1." "Individual covs t." "Mandate period FE" "Municipal FE" "Occupation mandate trend" "Workplace mandate trend" "R2" "Observations")) 
;
#delimit cr	



**********SNI************
***(Requested by Reviewers)

tostring Sni, gen(sni_str)
gen sni_str_2 = substr(sni_str, 1, 2)
destring sni_str_2, gen(sni_2)
sort sni_2

*Note that some sni:s dont exist
replace sni_2 = . if sni_2==0
egen sni_2_grp = group(sni_2) 

*Sector for sni from 2007
gen sni_1 = .
replace sni_1 = 1 if sni_2>=10 &sni_2<=33 & year>2006 /*manufacturing*/
replace sni_1 =2 if sni_2==35 & year>2006 /*electricity, gas, heat*/
replace sni_1 =2 if sni_2>=36 & sni_2<=39 & year>2006 /*Water/sanitation*/
replace sni_1=3 if sni_2>=41 & sni_2<=43 & year>2006 /*Construction*/
replace sni_1=4 if sni_2>=45 & sni_2<=47 & year>2006 /*Trade*/
replace sni_1=5 if sni_2>=49 & sni_2<=53 & year>2006 /*transport*/
replace sni_1=6 if sni_2==55 | sni_2==56 & year>2006 /*Hotel/restaur*/
replace sni_1=7 if sni_2>=58 & sni_2<=63 & year>2006 /*inf and communic */
replace sni_1=8 if sni_2>=64 & sni_2<=66 & year>2006 /*Finance*/
replace sni_1=9 if sni_2==68 & year>2006 /*Real estate*/
replace sni_1=10 if sni_2>=69 & sni_2<=75 & year>2006 /*Law, econ, science, STEM*/
*replace sni_1=11 if sni_2>=77 & sni_2<=82 & year>2006 /*Rental, travel, Service*/
replace sni_1=11 if sni_2==84 & year>2006 /*Public adm, defence, social sec*/
replace sni_1=12 if sni_2==85 & year>2006 /*Education*/
replace sni_1=13 if sni_2>=86 & sni_2<=88 & year>2006 /*Health care*/
replace sni_1=14 if sni_2>=90 & sni_2<=93 & year>2006 /*Culture*/
replace sni_1=15 if sni_2>=94 & sni_2<=96 & year>2006 /*Other Service*/
replace sni_1=16 if sni_2==97 | sni_2==98 & year>2006 /*Household prod*/
replace sni_1=17 if sni_2==99 & year>2006 /*International*/

*2002
replace sni_1 = 1 if sni_2>=15 &sni_2<=36 & year>=2002 & year<=2006 /*manufacturing*/
replace sni_1 =2 if sni_2==40 & year>=2002 & year<=2006 /*electricity, gas, heat*/
replace sni_1 =2 if sni_2==41 | sni_2==37 & year>=2002 & year<=2006 /*Water*/
replace sni_1=3 if sni_2==45 & year>=2002 & year<=2006 /*Construction*/
replace sni_1=4 if sni_2>=50 & sni_2<=52 & year>=2002 & year<=2006 /*Trade*/
replace sni_1=5 if sni_2>=60 & sni_2<=64 & year>=2002 & year<=2006 /*transport*/
replace sni_1=6 if sni_2==55 & year>=2002 & year<=2006 /*Hotel/restaur*/
replace sni_1=7 if sni_2==72 & year>=2002 & year<=2006  /*inf and communic */
replace sni_1=8 if sni_2>=65 & sni_2<=67 & year>=2002 & year<=2006  /*Finance*/
replace sni_1=9 if sni_2==70 & year>=2002 & year<=2006 /*Real estate*/
replace sni_1=10 if sni_2>=73 & sni_2<=74 & year>=2002 & year<=2006 /*Law, econ, science, STEM*/
*replace sni_1=11 if sni_2==71 | sni_2==93 & year>=2002 & year<=2006  /*Rental, travel, Service*/
replace sni_1=11 if sni_2==75 & year>=2002 & year<=2006  /*Public adm, defence, social sec*/
replace sni_1=12 if sni_2==80 & year>=2002 & year<=2006 /*Education*/
replace sni_1=13 if sni_2==85 & year>=2002 & year<=2006 /*Health care*/
replace sni_1=14 if sni_2==92 & year>=2002 & year<=2006 /*Culture*/
replace sni_1=15 if sni_2==91 & year>=2002 & year<=2006 /*Other Service*/
replace sni_1=16 if sni_2==95 & year>=2002 & year<=2006 /*Household prod*/
replace sni_1=17 if sni_2==99 & year>=2002 & year<=2006 /*International*/


*Matrix

matrix A = J(20,8,.)


forvalues y=1/15 {

reghdfe $depvar $indvar $covs $wpssykcovslag if sni_1==`y' & $restriction, absorb(identificationvar mandatperiod##occupation mandatperiod##wp Kkod) cluster(identificationvar)


*Values in Matrix 

            matrix A[`y',1] = _b[maintreatvar]
                     matrix A[`y',2] = _b[maintreatvar] + 1.96*_se[maintreatvar]
                     matrix A[`y',3] = _b[maintreatvar] - 1.96*_se[maintreatvar]
                     matrix A[`y',4] = `y'
                     matrix A[`y',5] = e(N)          
}




*Place mean of independent var in A6
forvalues y=1/15 {
estpost summarize maintreatvar if sni_1==`y'
 matrix A[`y',6] = e(mean) 
}

forvalues y=1/15 {
estpost summarize gender if sni_1==`y'
 matrix A[`y',7] = e(mean) 
}

forvalues y=1/15 {
estpost summarize yearseducation if sni_1==`y'
 matrix A[`y',8] = e(mean) 
}

svmat A

rename A1 estimate
rename A2 upper
rename A3 lower
rename A4 sni
rename A5 obs
rename A6 mean_treat
rename A7 mean_sex
rename A8 mean_education


save "E:/ProjData/Workplace, elite/sector", replace

*Keep matrix
drop if sni==.
 
sort estimate
gen n = _n

label define sni 1 "Manufacturing" 2 "Water El Heat" 3 "Construction" 4 "Trade" 5 "Transport" 6 "Hotel Rest." 7 "Communication" 8 "Finance" 9 "Real Estate" 10 "Law Econ STEM" 11 "Public Adm" 12 "Education" 13 "Health Care" 14 "Culture" 15 "Service"
gen sni_lab = "Manufact" if sni==1
replace sni_lab = "Infr" if sni==2
replace sni_lab = "Constr" if sni==3
replace sni_lab = "Trade" if sni==4
replace sni_lab = "Transport" if sni==5
replace sni_lab = "Tourism" if sni==6 
replace sni_lab = "Communic" if sni==7
replace sni_lab = "Finance" if sni==8
replace sni_lab = "RealEstate" if sni==9
replace sni_lab = "EconSTEM" if sni==10
replace sni_lab = "Public" if sni==11
replace sni_lab = "Educ" if sni==12
replace sni_lab = "Health" if sni==13
replace sni_lab = "Culture" if sni==14
replace sni_lab = "Service" if sni==15  


sum estimate

*Weight: 
egen total = total(obs)
gen sect_size = (obs/total)*100


graph twoway rcap upper lower n, lstyle(ci) ||  scatter estimate n [w=sect_size], msize(0.8) mstyle(p1) legend(off) graphregion(color(white)) ytitle(Coefficient Size) xtitle(Sorted from lowest to highest)
graph export "C:\Userdata\Shared\Output\Workplace, elite\sni_1_w.pdf", replace


****
clear all
clear matrix
capture log close																		
